package com.competition.web.admin.util;

import org.springframework.context.ApplicationContext;

/**
 * 类名： SpringBeanUtil 概要： Spring Bean工具类
 */
public class SpringBeanUtil {

	private static ApplicationContext ac = null;

	public static ApplicationContext getApplicationContext() {
		return ac;
	}

	public static void setApplicationContext(ApplicationContext ac) {
		SpringBeanUtil.ac = ac;
	}

	/**
	 * 根据bean名获取bean对象。
	 *
	 * @param beanName bean名
	 * @return bean对象
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName) {

		// 从容器中获取bean
		try {
			return (T) ac.getBean(beanName);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 根据bean名获取bean对象。
	 *
	 * @param beanName bean名
	 * @param beanType bean类
	 * @return bean对象
	 */
	public static <T> T getBean(String beanName, Class<T> beanType) {

		// 从容器中获取bean
		try {
			return ac.getBean(beanName, beanType);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 根据bean类获取bean对象。
	 *
	 * @param beanType bean类
	 * @return bean对象
	 */
	public static <T> T getBean(Class<T> beanType) {

		// 从容器中获取bean
		try {
			return ac.getBean(beanType);
		} catch (Exception e) {
			return null;
		}
	}

}
